package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.MarkiMapper;
import com.gtq.uhome.service.DistributionPortSerVice;
import com.gtq.uhome.service.MarkiService;
import com.gtq.uhome.vo.DeliveryOrderManagementVo;
import com.gtq.uhome.vo.MarkiVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配送员管理业务层
 */
@Service("markiService")
public class MarkiServiceImpl implements MarkiService {

    @Resource
    MarkiMapper markiMapper = null;

    @Resource
    DistributionPortSerVice distributionPortSerVice;

    //配送员管理。查询所有
    @Override
    public List<MarkiVo> findAll() {

        return markiMapper.findAll();
    }

    //模糊查询
    @Override
    public List<MarkiVo> findLike(MarkiVo markiVo) {

        return markiMapper.findLike(markiVo);
    }

    //添加配送员信息,staff表,vehicle表,staff_role表
    @Override
    public Boolean insertMarki(MarkiVo markiVo) {

        Integer lines2 = -1;
        Integer lines3 = -1;

        markiVo.setStatusCode("0");
        //设置用户名与初始密码
        markiVo.setStaffNumber(markiVo.getStaffPhoneNumber());

        markiVo.setStaffPassword("123456");
        Integer lines1 = markiMapper.insertMarkiInStaff(markiVo);
        if (lines1 == 1) {
            Integer staffId = markiMapper.selectIdByStaffPhoneNumberInStaff(markiVo.getStaffPhoneNumber());
            markiVo.setStaffId(staffId);
            lines2 = markiMapper.insertMarkiInVehicle(markiVo);
            if (lines2 == 1) {
                lines3 = markiMapper.insertMarkiInStaffRole(markiVo);
            }
        }
        return lines1 == 1 && lines2 == 1 && lines3 == 1;
    }

    //删除配送员信息
    @Override
    public Boolean deleteMarki(List<MarkiVo> staffId) {
        Integer lines = -1;
        for (MarkiVo markiVo : staffId) {
            lines = markiMapper.deleteMarki(markiVo.getStaffId());
        }
        return lines == 1;
    }

    //修改状态，是否冻结，0：未冻结，1：冻结
    @Override
    public Boolean freeze(List<MarkiVo> staffId) {
        Integer lines = -1;
        for (MarkiVo markiVo : staffId) {
            lines = markiMapper.freeze(markiVo.getStaffId());
        }
        return lines == 1;
    }

    //解冻
    @Override
    public Boolean notFreeze(List<MarkiVo> staffId) {
        Integer lines = -1;
        for (MarkiVo markiVo : staffId) {
            lines = markiMapper.notFreeze(markiVo.getStaffId());
        }
        return lines == 1;
    }

    //修改配送员信息
    @Override
    public Boolean updateMarki(MarkiVo markiVo) {
        Integer lines2 = -1;
        Integer lines1 = markiMapper.updateMarkiInStaff(markiVo);
        if (lines1 == 1) {
            lines2 = markiMapper.updateMarkiInVehicle(markiVo);
        }
        return lines1 == 1 && lines2 == 1;
    }

    //判断配送员电话是否重复
    @Override
    public Boolean isRepeat(String staffPhoneNumber) {
        List<MarkiVo> all = markiMapper.findAll();
        for (MarkiVo markiVo : all) {
            String oldStaffPhoneNumber = markiVo.getStaffPhoneNumber();
            if (staffPhoneNumber.equals(oldStaffPhoneNumber)) {
                //表示重复
                return true;
            }
        }
        //表示不重复
        return false;
    }

    /**
     * 查询配送员接单数量
     * -----李龙
     *
     * @return 返回派送员临时对象集合
     */
    @Override
    public List<MarkiVo> fandAllMarki() {
        //调用查询全部派送员
        List<MarkiVo> all = findAll();
        //查询到结果
        if (all.size() > 0) {
            return queryOrderQuantity(all);
        }
        //没有查询到
        return null;
    }


    /**
     * 通过派送员电话号码和派送员名字
     * 模糊查询全部配送员接单数量
     * -----李龙
     *
     * @param markiVo 用于装载字段的对象
     * @return 返回模糊查询结果
     */
    @Override
    public List<MarkiVo> vagueFandAllMarki(MarkiVo markiVo) {
        List<MarkiVo> like = findLike(markiVo);
        //模糊查询到结果
        if (like.size() > 0) {
            return queryOrderQuantity(like);
        }
        return null;
    }

    @Override
    public List<MarkiVo> vagueFandAllMarki(String vagueFind) {
        List<MarkiVo> markiVos = markiMapper.vagueFand(vagueFind);
        //模糊查询到结果
        if (markiVos != null) {
            return queryOrderQuantity(markiVos);
        }
        //没有模糊查询到结果
        return null;

    }

    /**
     * 平台派单方法 一个订单不能被两个派送员派送
     * -----李龙
     *
     * @param courierId 派送员id
     * @param orderId   订单号id
     * @return 返回true派单成功 false派单失败
     */
    @Override
    public Boolean platformDispatch(Integer courierId, String orderId) {
        //调用抢单方法
        return distributionPortSerVice.setReceivingOrders(courierId, orderId);
    }

    /**
     * 查询未派单的订单
     * ----李龙
     *
     * @return 返回一个临时vo
     */
    @Override
    public List<DeliveryOrderManagementVo> selectUndelivered() {
        //调用Mapper方法
        List<DeliveryOrderManagementVo> deliveryOrderManagements = markiMapper.selectUndelivered();
        if (deliveryOrderManagements.size() > 0)
            return deliveryOrderManagements;
        else
            return null;
    }

    /**
     * 派送列表 未派单的模糊查询
     *
     * @param vagueSelect 临时对象
     * @return 返回查询数据
     */
    @Override
    public List<DeliveryOrderManagementVo> vagueSelectUndelivered(String vagueSelect) {
        List<DeliveryOrderManagementVo> deliveryOrderManagementVos = markiMapper.vagueSelectUndelivered(vagueSelect);
        if (deliveryOrderManagementVos != null)
            return deliveryOrderManagementVos;
        return null;
    }

    @Override
    public Map orderInfo(String oderId, Integer courierId) {
        //生成一个map
        Map oderInfo = new HashMap();
        //查询配送员信息
        MarkiVo byStaffId = markiMapper.findByStaffId(courierId);
        //查询订单员信息
        List<DeliveryOrderManagementVo> deliveryOrderManagementVos = markiMapper.selectUndeliveredByOrderId(oderId);
        //添加到map中
        oderInfo.put("orderInfo", deliveryOrderManagementVos);
        oderInfo.put("courierId", byStaffId);
        if (byStaffId != null && deliveryOrderManagementVos != null) {
            return oderInfo;
        }
        return null;
    }

    /**
     * 查询派送员当前接单数
     * -----李龙
     *
     * @param list 派送员名单
     * @return 返回派送员接单数量
     */
    public List<MarkiVo> queryOrderQuantity(List<MarkiVo> list) {
        //遍历全部查询
        for (int i = 0; i < list.size(); i++) {
            //获取派送员ID
            Integer staffId = list.get(i).getStaffId();
            //查询派送员当前接单数量
            list.get(i).setCurrentOrders(markiMapper.queryOrderQuantity(staffId));
            //设置与站点距离
            list.get(i).setStationDistance((int) (Math.random()) * 20);

        }
        //返回结果
        return list;
    }

}
